import 'package:dio/dio.dart';
import 'package:htxd_flutter_mapbox_sdk/htxd_flutter_mapbox_sdk.dart';
import 'package:htxd_flutter_mapbox_sdk/search/search_poi_entity.dart';

/// 高德poi搜索
class AmapSearchUtil {
  /// poi 搜索
  static Future<List<SearchPoiEntity>> searchPoi(String keyWord) async {
    List<SearchPoiEntity> result = [];
    var url =
        'https://restapi.amap.com/v5/place/text?key=${InitConfig.amapSearchKey}&keywords=$keyWord&page_size=25';
    print(url);
    final response = await Dio().get(url);
    if (response.statusCode == 200) {
      var data = response.data;
      print(data);
      int? count = int.tryParse(data["count"]);
      if (count != null && count > 0) {
        if (data["infocode"] == "10000") {
          data["pois"].forEach((v) {
            result.add(SearchPoiEntity.fromJson(v));
          });
        }
      } else {
        print("高德搜索key可能有误");
      }
    }
    return result;
  }
}
